package cn.lee.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : lichl(李承霖)
 * create: 2021/4/15 11:10
 */

@Configuration
@EnableSwagger2
public class SpringSwiggerConfig{
  
  
  @Bean
  public Docket createRestApi(){
    return new Docket(DocumentationType.SWAGGER_2)
      //apiInfo指定测试文档基本信息，这部分将在页面展示
      .apiInfo(apiInfo())
      .select()
      //apis() 控制哪些接口暴露给swagger，
      // RequestHandlerSelectors.any() 所有都暴露
      // RequestHandlerSelectors.basePackage("com.info.*")  指定包位置
      .apis(RequestHandlerSelectors.basePackage("cn.lee.test"))
      .paths(PathSelectors.any())
      .build().globalOperationParameters(this.getParamList());
  }

    //添加请求头等自定义参数
  private List<Parameter> getParamList(){
    ParameterBuilder tokenParam = new ParameterBuilder();
    List<Parameter> args = new ArrayList<>();
    tokenParam.name("auth").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(true).build();
    args.add(tokenParam.build());
    return args;
  }
  
  //基本信息，页面展示
  private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
      .title("jtw_demo")
      .description("jwt的接口文档")
      .build();
  }
  
}
